<?php 
session_start();
require_once ("class/m_customer_history.php");
require_once ("class/m_stock.php");
	$m = new M_customer_history();
	$m_stock=new M_stock();
	$UserName=$_SESSION['username'];
	$BranchID=$_SESSION['bid'];
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'info'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				echo json_encode($m->getDataInfo($id));
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']=='chkstock'){			
			$sid=$_POST['id'];
			$amt=$_POST['amt'];
			//check stock
			$reValue=$m->getProductDetail($sid, $BranchID,$amt);
			
			if ($reValue['result']==0){
				if ($reValue['count']>0){
					$ar_result=array('data'=> json_encode($reValue) ,
									'rows'=> 9999,
									'date'=>date('d/m/Y'),
									'csid'=>0
									);
					echo json_encode( $ar_result );					
				}else{
					$ar_result=array('data'=> "",
											'rows'=> 0,
											'date'=>0,
											'csid'=>0
											);
					echo json_encode( $ar_result );						
				}
			}else{
			
				$ar_result=array('data'=> "",
										'rows'=> 0,
										'date'=>0,
										'csid'=>0
										);
				echo json_encode( $ar_result );		
			}
			
		}else if ($_POST['m']=='add'){
			$id=$_POST['id'];
			$sid=$_POST['sid'];
			$remark=$_POST['remark'];
			$approvedBy=$_POST['approvedby'];
			
			if (!(isset($approvedBy)) || $approvedBy=='') {
					$approvedBy = $UserName;
			}
			
			$reValue=$m->getProductDetail($sid, $BranchID);
  
			if ($reValue['result']==0){
				if ($reValue['count']>0){
					//Course've treament and set inside
					$ar_result=array('data'=> json_encode($reValue) ,
									'rows'=> 9999,
									'date'=>date('d/m/Y'),
									'csid'=>0
									);
					echo json_encode( $ar_result );	
				}else{
					//Only course
					$csid=$m->addService($sid,$BranchID,$remark,$approvedBy );
					
					$result=$m->getAccessService($sid);
					$rows=$m->getRowCount();
					$html='';
					if ($rows>0){
						$k=1;
						while($row = $result->FetchRow()){
							If($k%2 <> 0){
								$html .= "<tr style='background-color:#f4f4f4;height:20px;'>";
							} else{
								$html .= "<tr style='background-color:#fff;height:20px;'>";
							}
							
							$html.=genService($row,$k)."</tr>";
		 
							$k++;				
						}
					}
					$ar_result=array('data'=>$html,
									'rows'=>$rows,
									'date'=>date('d/m/Y'),
									'csid'=>$csid
									);
					echo json_encode( $ar_result );
				}
			}else{
					$csid=$m->addService($sid,$BranchID,$remark,$approvedBy );
					//get set id ,
					$result_ID=$m->getSetTreatmentInCourse($sid);
					while ($rowVal=$result_ID->FetchRow()){
						$m_stock->Stock_Set_Treatment_Update($rowVal['ID'],$BranchID , 1, $UserName,0);
					}
					
					$result=$m->getAccessService($sid);
					$rows=$m->getRowCount();
					$html='';
					if ($rows>0){
						$k=1;
						while($row = $result->FetchRow()){
							If($k%2 <> 0){
								$html .= "<tr style='background-color:#f4f4f4;height:20px;'>";
							} else{
								$html .= "<tr style='background-color:#fff;height:20px;'>";
							}
							
							$html.=genService($row,$k)."</tr>";
		 
							$k++;				
						}
					}
					$ar_result=array('data'=>$html,
									'rows'=>$rows,
									'date'=>date('d/m/Y'),
									'csid'=>$csid
									);
					echo json_encode( $ar_result );
			}
			
		}else if ($_POST['m']=='del'){
			$id=$_POST['id'];
			$rid=$_POST['rid'];
			$amt=$_POST['amt'];
			
			$m->delService($id,$rid ,$amt);
			
			$result_ID=$m->getSetTreatmentInCourse($id);
			while ($rowVal=$result_ID->FetchRow()){
				$m_stock->Stock_Set_Treatment_Update($rowVal['ID'],$BranchID , $amt, $UserName,1);
			}
					
			$result=$m->getAccessService($id);
	
			$rows=$m->getRowCount();
			$html='';
			if ($rows>0){
				$k=1;
				while($row = $result->FetchRow()){
					If($k%2 <> 0){
						$html .= "<tr style='background-color:#f4f4f4;height:20px;'>";
					} else{
						$html .= "<tr style='background-color:#fff;height:20px;'>";
					}
					
					$html.=genService($row,$k)."</tr>";
 
					$k++;				
				}
			}
			$ar_result=array('data'=>$html,
							'rows'=>$rows
							);
			echo json_encode( $ar_result );	
			
		}else if ($_POST['m']== 'detail'){
			$id=$_POST['id'];
			$result=$m->getAccessService($id);
			
			$rows=$m->getRowCount();
			$html='';
			if ($rows>0){
				$k=1;
				while($row = $result->FetchRow()){
					If($k%2 <> 0){
						$html .= "<tr style='background-color:#f4f4f4;height:20px;'>";
					} else{
						$html .= "<tr style='background-color:#fff;height:20px;'>";
					}
					
					$html.=genService($row,$k)."</tr>";
 
					$k++;				
				}
			}
			
			$ar_result=array('data'=>$html,
							'rows'=>$rows
							);
			echo json_encode( $ar_result );
			
		}else if ($_POST['m']== 'updateall'){				
			$ar=$_POST['list'];
			
			foreach($ar as $val){ 
			
				$m->addService($val['csid'],$BranchID,"", $UserName ,$val['amount'] );
				
				 $result_ID=$m->getSetTreatmentInCourse($val['csid']);
				 while ($rowVal=$result_ID->FetchRow()){
					 $m_stock->Stock_Set_Treatment_Update($rowVal['ID'],$BranchID , $val['amount'], $UserName,0);
				 }
			}
			echo json_encode( $ar );//$html;
		
		}else if ($_POST['m']== 'getServiceUpdate'){			
			$result=$m->getServiceAllData($_POST['id']); 
			
			$rows=$m->getRowCount();
		
			if ($rows>0){
					while($row = $result->FetchRow()){
						$html .= "<tr style='background-color:#fff;'>";
						$html.=genUpdateService($row)."</tr>";
	 
						$k++;
					}
			}else{
					$html="<tr style='background-color:#fff;'>"
					."<td colspan='5' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
					$rows=0;
			}
				//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							);
			 echo json_encode( $ar_result );//$html;
			 
			 
		}else if ($_POST['m']== 'service'){
		
			$page=$_POST['page'];
			$page=isset($page)?$page:1;
			
			$result=$m->getServiceData($_POST['id'],$page); ///CustId, page
		
			$rows=$m->getRowCount();
			
			$k=1 + (($page-1) * $m->getListPage());

			if ($rows>0){
				while($row = $result->FetchRow()){
					If($k%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					
					$html.=genTable($row,$k)."</tr>";
 
					$k++;
				}
			}else{
					$html="<tr>"
					."<td colspan='10' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage()
							);
			 echo json_encode( $ar_result );//$html;
					
		}
	}else{echo"Error2";}

	function genService($row,$k){
		$html.="<td align='right'>".$k."</td>";
		$html.="<td style='display:none'>".$row['id']."</td>";
		$html.="<td  align='center' >".$row["_ApprovedDate"]."</td>";
		$html.="<td  align='left' >".$row['BranchName']."</td>";
		$html.="<td align='left'>".$row['ApprovedBy']."</td>";
		$html.="<td  align='left' >".$row["Remark"]."</td>";
		$html.="<td  align='Right' >".$row["Amount"]."</td>";
		$html.="<td  align='center' ><img src='images/remove16.png' style='cursor:pointer' class='btnRemove' border=0/></td>";
		
		return $html;
	}
	
	function genUpdateService ($row){
		$html.="<td style='display:none'>".$row['id']."</td>";
		$html.="<td align='left' style='padding-left:5px;'>".$row['ProductName']."</td>";
		$html.="<td align='center'>".$row['ExpiredDate']."</td>";
		$html.="<td align='right'><input type='text' style='text-align:right;font-size:11px;border:1px solid #ccc;padding:2px;' name='txtUsed' maxlength='3' size='3' value='0' /></td>";
		$html.="<td align='right' style='font-size:12px;color:red'>".$row['Remain']."</td>";
		return $html;
	}
	function genTable($row,$k){
	global $posScreen;
		$html.="<td align='right'>".$k."</td>";
		$html.="<td style='display:none'>".$row['id']."</td>";
		$html.="<td style='display:none'>".$row['BranchID_Sale']."</td>";
		$html.="<td align='left' style='padding-left:5px;'>".$row['ProductName']."</td>";
		$html.="<td align='left' tyle='padding-left:5px;'>".$row['BranchName']."</td>";
		$html.="<td align='center'>".$row['_LastAccess']."</td>";
		
	    if ($row['checkExpire'] =='1'){
			$html.="<td align='center'><font color=red>".$row['ExpiredDate']."</font></td>";
			$html.="<td align='right'>".$row['Times']."</td>";
			$html.="<td align='right' style='font-size:12px;color:red'>".$row['Remain']."</td>";
			$html.="<td align='center'>...</td>";
		}else{
			$html.="<td align='center'>".$row['ExpiredDate']."</td>";
			$html.="<td align='right'>".$row['Times']."</td>";
			$html.="<td align='right' style='font-size:12px;color:red'>".$row['Remain']."</td>";
			
			if ($_SESSION["permission"][$posScreen]["Add"]=="1") { 
				$html.="<td align='center'><img src='images/confirm.gif' border='0' class='btnUsed' style='cursor:pointer' /></td>";			
			}else{
				 $html.="<td align='center'></td>";
			}
		}
		
		return $html;
	}

?>